package 构造特定数组的逆序拼接.src.main;// package 构造特定数组的逆序拼接.src.main;

import java.util.Arrays;

public class Main3 {
    public static int[] solution(int n) {
        // 计算结果数组的总长度
        int length = 0;
        for (int i = 1; i <= n; i++) {
            length += n - i + 1;
        }

        // 创建结果数组
        int[] result = new int[length];
        int index = 0;

        // 构建数组
        for (int i = 1; i <= n; i++) {
            for (int j = n; j >= i; j--) {
                result[index++] = j;
            }
        }

        return result;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(solution(3))); // 应输出 [3, 2, 1, 3, 2, 3]
        System.out.println(Arrays.toString(solution(4))); // 应输出 [4, 3, 2, 1, 4, 3, 2, 4, 3, 4]
        System.out.println(Arrays.toString(solution(5))); // 应输出 [5, 4, 3, 2, 1, 5, 4, 3, 2, 5, 4, 3, 5, 4, 5]
        System.out.println(Arrays.equals(solution(3), new int[] { 3, 2, 1, 3, 2, 3
        }));
        System.out.println(Arrays.equals(solution(4), new int[] { 4, 3, 2, 1, 4, 3,
                2, 4, 3, 4 }));
        System.out.println(Arrays.equals(solution(5), new int[] { 5, 4, 3, 2, 1, 5,
                4, 3, 2, 5, 4, 3, 5, 4, 5 }));
    }
}